package com.cloud.pole.common.util;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by ace on 2017/9/10.
 */
public class StringHelper {
    public static String getObjectValue(Object obj) {
        return obj == null ? "" : obj.toString();
    }
    
    /**
               * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(Map<String, String> params) {
        String prestr = "";
        try {
            List<String> keys = new ArrayList<String>(params.keySet());
            Collections.sort(keys);
            for (int i = 0; i < keys.size(); i++) {
                String key = keys.get(i);
                String value = params.get(key);
                value = URLEncoder.encode(value, "UTF-8");
                if (i == keys.size() - 1) {
                    //拼接时，不包括最后一个&字符
                    prestr = prestr + key + "=" + value;
                } else {
                    prestr = prestr + key + "=" + value + "&";
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return prestr;
    }

    /**
              * 获取带参数前地址
     * @param url
     * @return
     */
    public static String getBaseUrl(String url) {
        String baseUrl = null;
        try{
            int index = url.indexOf("?");
            if(index > 0){
                baseUrl = url.substring(0, index);
            }
            else{
                baseUrl = url;
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        return  baseUrl;
    }    
    
    private static final Pattern PATTERN = Pattern.compile("^(([1-9]{1}\\\\d*)|([0]{1}))(\\\\.(\\\\d){0,2})?$");

    public static void main(String[] args) {
    	String reg = "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$";
    	String reg1 = "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,10})?$";    			 
    	String str = "199.99999999";
    	System.out.println(str.replaceAll(reg1, "111"));
    }
}
